﻿@using Jx.Toolbox.Extensions
@using JxAudio.Front.Data
@using JxAudio.TransVo
@inject HttpClient Http
@inject NavigationManager NavigationManager
@inject IDispatchService<AddTrackMessage> DispatchService

<AutoFill TValue="SearchList" Debounce="2000" OnCustomFilter="OnCustomFilter" OnSelectedItemChanged="OnSelectedItemChanged"
          OnGetDisplayText="@(list => list?.Name ?? "")" IsSelectAllTextOnFocus="true">
    <Template>
        <div class="d-flex">
            <div>
                <img alt="@context.Name" style="width: 60px;height: 60px" src="@($"/api/Cover?coverId={context.Id}")" class="bb-avatar" />
            </div>
            <div class="ps-2">
                <div>@context.Name</div>
                <div><Tag>@context.SearchType.ToDisplayName()</Tag></div>
            </div>
        </div>
    </Template>
</AutoFill>

@code {
    
    private async Task<IEnumerable<SearchList>> OnCustomFilter(string searchText)
    {
        var list = await Http.GetFromJsonAsync<SearchResultVo>("/api/Recommend/Search?query=" + searchText);
        if (list == null)
        {
            return [];
        }
        
        var searchLists = new List<SearchList>();

        if (list.Tracks != null)
        {
            foreach (var track in list.Tracks)
            {
                var searchList = new SearchList
                {
                    Id = track.Id,
                    Name = track.Title.IsNullOrEmpty() ? track.Name : track.Title,
                    CoverId = track.CoverId,
                    SearchType = SearchType.Track
                };
                searchLists.Add(searchList);
            }
        }
        
        if (list.Albums != null)
        {
            foreach (var album in list.Albums)
            {
                var searchList = new SearchList
                {
                    Id = album.Id,
                    Name = album.Title,
                    CoverId = album.CoverId,
                    SearchType = SearchType.Album
                };
                searchLists.Add(searchList);
            }
        }
        
        if (list.Artists != null)
        {
            foreach (var artist in list.Artists)
            {
                var searchList = new SearchList
                {
                    Id = artist.Id,
                    Name = artist.Name,
                    CoverId = artist.CoverId,
                    SearchType = SearchType.Artist
                };
                searchLists.Add(searchList);
            }
        }

        return searchLists;
    }

    private async Task OnSelectedItemChanged(SearchList arg)
    {
        switch (arg.SearchType)
        {
            case SearchType.Artist:
                NavigationManager.NavigateTo($"/artist/{arg.Id}");
                break;
            case SearchType.Album:
                NavigationManager.NavigateTo($"/album/{arg.Id}");
                break;
            case SearchType.Track:
                var track = await Http.GetFromJsonAsync<TrackVo>("/api/track/?id=" + arg.Id);
                if (track == null)
                {
                    return;
                }
                DispatchService.Dispatch(new DispatchEntry<AddTrackMessage>()
                {
                    Entry = new AddTrackMessage()
                    {
                        Tracks = [track],
                        Type = PlayType.Replace
                    }
                });
                
                break;
        }
    }

}